package com.hilon.aixinmis1.base.key_value;

import com.hilon.foundation.base.BaseServiceImpl;
import com.hilon.foundation.util.HilonTools;
import com.hilon.foundation.util.ResultBean;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import jakarta.transaction.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class KeyValueServiceImpl
        extends BaseServiceImpl<KeyValue, KeyValue>
        implements KeyValueService {
    public KeyValueServiceImpl(EntityManager entityManager) {
        super(entityManager);
        this.entityName = "KeyValue";
        this.viewName = "ViewKeyValue";
    }



    @Transactional
    public ResultBean<String> deleteByIds(List<Long> ids) {
        String idsStr = HilonTools.getIdsStr(ids);

        String sql = "update " +
                entityName +
                " e set e.isDeleted=true " +
                "where e.id in (" + idsStr + ") ";
        try {
            Query query = entityManager.createQuery(sql);
            int i = query.executeUpdate();
            if (i > 0)
                return new ResultBean<>(200, "删除成功");
            else
                return new ResultBean<>(501, "删除失败,该记录已审核");
        } catch (Exception e) {
            System.out.println(e);
            return new ResultBean<>(501, "删除程序失败");
        }
    }
}
